package com.haimi.shiguang.utils;

import android.text.TextUtils;
import android.util.Log;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * @author qinwei
 * @date 2017/1/18
 */

public class DateTimeUtil {
    public static final String DATE_YYYYMMDDHHMM = "yyyy-MM-dd HH:mm";
    public static final String DATE_YYYYMMDD = "yyyyMMdd";
    public static final String DATE_YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
    public static final String DATE_YYYYMMDDHHMMSSSSS = "yyyyMMddHHmmssSSS";

    public static long getMillisBetween(String smdate, String bdate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_YYYYMMDDHHMM, Locale.SIMPLIFIED_CHINESE);
        Calendar cal = Calendar.getInstance();
        cal.setTime(sdf.parse(smdate));
        long time1 = cal.getTimeInMillis();
        cal.setTime(sdf.parse(bdate));
        long time2 = cal.getTimeInMillis();
        return time2 - time1;
    }

    public static String getCurrentDateTime() {
        return getTime(System.currentTimeMillis(), "yyyyMMddHHmm");
    }

    public static String getCurrentDateTime(String pattern) {
        return getTime(System.currentTimeMillis(), pattern);
    }

    /**
     * 通过时间获得long
     *
     * @param time
     * @return
     * @throws ParseException
     */
    public static long getLongByTime(String time, String pattern) {
        Calendar cal = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(pattern, Locale.SIMPLIFIED_CHINESE);
            cal = Calendar.getInstance();
            cal.setTime(sdf.parse(time));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long time1 = cal.getTimeInMillis() / 1000;
        return time1;
    }


    public static int getMMBetween(String smdate, String bdate) throws ParseException {
        long millis = getMillisBetween(smdate, bdate);
        long between_mm = millis % 60000L > 0 ? millis / 60000L + 1 : millis / 60000L;
        return StringUtil.toInt(String.valueOf(between_mm));
    }

    public static Date strToDate(String dateStr) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return format.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date strToDate(String dateStr, String pattern) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        try {
            return format.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String dateFormat(String dateStr, String pattern) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(strToDate(dateStr));
    }

    public static String dateFormat(Date date, String pattern) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(date);
    }


    public static String getTime(String dateStr) {
        return dateFormat(dateStr, "HH:mm");
    }

    public static String getTime(String dateStr, String pattern) {
        return dateFormat(dateStr, pattern);
    }

    public static String getTime(long time) {
        Date date = new Date(time);
        return dateFormat(date, DATE_YYYYMMDDHHMM);
    }

    public static String getTime(long time, String pattern) {
        Date date = new Date(time);
        return dateFormat(date, pattern);
    }


    /**
     * 判断 time 是否在 startTime 和 end Time 之间
     *
     * @return
     */
    public static boolean isBetweenMillis(long startTime, long endTime, long date) {
        return date >= startTime && date <= endTime;
    }


    /**
     * 得到给定日期N天后的日期
     *
     * @param num
     * @return
     */
    public static void doMillisAfter(String datestr, int num) {
        String pattern = "yyyy-MM-dd";
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        try {
            Date date1 = format.parse(datestr);
            long time = date1.getTime() + (1000L * 60 * 60 * 24 * num);
            Date date = new Date();
            if (time > 0) {
                date.setTime(time);
            }
            System.out.println(format.format(date));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 得到给定日期N天前的日期
     *
     * @param num
     * @return
     */
    public static String doMillisBefore(String datestr, int num) {
        String pattern = "yyyy-MM-dd";
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        try {
            Date date1 = format.parse(datestr);
            long time = date1.getTime() - (1000L * 60 * 60 * 24 * num);
            Date date = new Date();
            if (time > 0) {
                date.setTime(time);
            }
            System.out.println(format.format(date));
            return format.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getTimeString(String time) {
        if (TextUtils.isEmpty(time)) {
            return "";
        } else {
            try {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.SIMPLIFIED_CHINESE);
                Date date = simpleDateFormat.parse(time);
                long formTime = date.getTime();
                long distance = (System.currentTimeMillis() - formTime) / 1000L;
                if (distance < 0L) {
                    distance = 0L;
                }

                String timestamp;
                if (distance < 60L) {
                    timestamp = "刚刚";
                } else if (distance < 3600L) {
                    distance /= 60L;
                    timestamp = distance + "分钟前";
                } else if (distance < 86400L) {
                    distance = distance / 60L / 60L;
                    timestamp = distance + "小时前";
                } else if (distance < 604800L) {
                    distance = distance / 60L / 60L / 24L;
                    timestamp = distance + "天前";
                } else if (distance < 2505600L) {
                    distance = distance / 60L / 60L / 24L / 7L;
                    timestamp = distance + "周前";
                } else if (distance < 7516800L) {
                    distance = distance / 60L / 60L / 24L / 30L;
                    timestamp = distance + "个月前";
                } else {
                    timestamp = time;
                }

                return timestamp;
            } catch (Exception var8) {
                Log.e("DateTimeUtil", var8.getMessage());
                return time;
            }
        }
    }
}
